package com.kycxika.app.activity.twoactivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.kycxika.app.R;
import com.kycxika.app.activity.LoginActivity;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.activity.base.BaseFragmentAdapter;
import com.kycxika.app.activity.fragment.ListFragment;
import com.kycxika.app.utils.SharedPreferencesHelp;
import com.kycxika.app.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

public class HomeTwoActivity extends BaseActivity {

    ViewPager mViewPager;
    List<Fragment> mFragments;
    Toolbar mToolbar;

    String[] mTitles = new String[]{
            "主页", "微博", "相册"
    };
    private AppBarLayout mAppBarLayout;
    private CollapsingToolbarLayout mCollapsingToolbarLayout;
    private Toolbar toolbar;
    private ImageView home_back;
    private CircleImageView home_img;
    private TextView home_name;
    private boolean userRegistionStatu;
    private ImageView imgbackdrop;

    @Override
    protected void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.CollapsingToolbarLayout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setInintView(toolbar);
        setupViewPager();

        imgbackdrop = (ImageView) findViewById(R.id.imgbackdrop);
    }

    private void setInintView(Toolbar toolbar) {
        home_back = (ImageView) toolbar.findViewById(R.id.home_back);
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeTwoActivity.this.finish();
            }
        });

        home_img = (CircleImageView) findViewById(R.id.home_img);
        home_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userRegistionStatu) {
                    startActivity(new Intent(HomeTwoActivity.this, MeActivity.class));
                    HomeTwoActivity.this.finish();
                }else {
                    startActivity(new Intent(HomeTwoActivity.this, LoginActivity.class));
                    HomeTwoActivity.this.finish();
                }
            }
        });

        home_name = (TextView) findViewById(R.id.home_name);
    }

    @Override
    protected void initData() {
        userRegistionStatu = SharedPreferencesHelp.getInstance().getLoginStatus();
        home_name.setText(SharedPreferencesHelp.getInstance().getLoginUsername());
        if(!TextUtils.isEmpty(SharedPreferencesHelp.getInstance().getUserHeader())) {
            /*Bitmap bitmap = BitmapFactory.decodeFile(SharedPreferencesHelp.getInstance().getUserHeader());
            header_image.setImageBitmap(bitmap);*/
            Glide.with(this)
                    .load(SharedPreferencesHelp.getInstance().getUserHeader())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                    .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                    .error(R.drawable.ce_01)
                    .into(new GlideDrawableImageViewTarget(home_img) {
                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                        }

                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                            super.onResourceReady(resource, animation);
                        }
                    });

            Glide.with(this)
                    .load(SharedPreferencesHelp.getInstance().getUserHeader())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                    .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                    .error(R.drawable.ce_01)
                    .into(new GlideDrawableImageViewTarget(imgbackdrop) {
                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                        }

                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                            super.onResourceReady(resource, animation);
                        }
                    });

        }else {
            home_img.setImageResource(R.drawable.ce_01);
        }
    }

    @Override
    protected void initListener() {
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if(verticalOffset != 0) {
                    home_img.setVisibility(View.VISIBLE);
                    home_name.setVisibility(View.VISIBLE);
                    home_img.setEnabled(true);
                }else {
                    home_img.setVisibility(View.INVISIBLE);
                    home_name.setVisibility(View.INVISIBLE);
                    home_img.setEnabled(false);
                }
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_home_two;
    }

    private void setupViewPager() {
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            ListFragment listFragment = ListFragment.newInstance(mTitles[i]);
            mFragments.add(listFragment);
        }
        BaseFragmentAdapter adapter =
                new BaseFragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);


        viewPager.setAdapter(adapter);
    }
}
